package com.friday.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.net.ssl.internal.ssl.Debug;

public class AuthorityFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
			
		HttpSession session = ((HttpServletRequest)request).getSession();
		
		String url = ((HttpServletRequest)request).getRequestURI();
				
		if(url.contains("My97DatePicker") || url.contains("resources") || url.endsWith("/") || url.endsWith("login.jsp") || url.endsWith("Error.jsp") ||  url.endsWith("LoginServlet.do")) {
			chain.doFilter(request, response);
		}
		else {
			String account = (String)session.getAttribute("account");
			
			if(account != null) {
				chain.doFilter(request, response);
			}
			else {
				((HttpServletResponse)response).sendRedirect("Error.jsp");
			}
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
